package com.qf.service.impl;

import com.qf.entity.Cart;
import com.qf.entity.Order;
import com.qf.mapper.OrderMapper;
import com.qf.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

//订单业务逻辑层
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public int addOrder(List<Cart> cartList) {
        Order order = new Order();
        int num = 0;
        int n=0;
        int u_id = 0;
        for (Cart c:cartList) {
            System.out.println("EEEEEEEEEEEE========"+c.getC_goodssubtotal());
            num+=c.getC_goodssubtotal();
            n=n+1;
            u_id = c.getU_id();
        }
        String o_id = UUID.randomUUID().toString();
        order.setO_id(o_id);
        order.setO_createtime(new Date());
        order.setU_id(u_id);
        order.setO_allprice(num);
        order.setO_allnumber(n);
        System.out.println("bbbbbbbbbbbbbbbbbb==========="+order);
        return orderMapper.addOrder(order);
    }

    /**
     * 通过用户的id查询订单
     * @param u_id
     * @return
     */
    @Override
    public List<Order> selOrderByUId(int u_id) {
        return orderMapper.selOrderByUId(u_id);
    }
}
